package com.android.mvpdemo.presenter;

import android.os.Handler;

import com.android.mvpdemo.bean.User;
import com.android.mvpdemo.model.IUserModel;
import com.android.mvpdemo.model.OnLoginListener;
import com.android.mvpdemo.model.UserModel;
import com.android.mvpdemo.view.IUserLoginView;

/**
 * Created by Lenovo on 2017/1/24.
 */
public class UserLoginPresenter {

    private IUserLoginView mIUserLoginView;
    private IUserModel mIUserModel;

    private Handler mHandler = new Handler();

    public UserLoginPresenter(IUserLoginView IUserLoginView) {
        mIUserLoginView = IUserLoginView;
        this.mIUserModel = new UserModel();
    }
    public void login(){
        mIUserLoginView.showLoading();
        mIUserModel.login(mIUserLoginView.getUserName(), mIUserLoginView.getPassword(), new OnLoginListener() {
            @Override
            public void loginSuccess(final User user) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mIUserLoginView.toMainActivity(user);
                        mIUserLoginView.hideLoading();
                    }
                });
            }

            @Override
            public void loginFailed(final String msg) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mIUserLoginView.showFailedError(msg);
                        mIUserLoginView.hideLoading();
                    }
                });
            }
        });
    }

    public void clear(){
        mIUserLoginView.clearUserName();
        mIUserLoginView.clearPassword();
    }
}
